package com.example.project;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.project.db.DBHelper;
import com.example.project.db.MyDBHelper;
import com.example.project.dto.ListnoCategoryDto;

public class QueryListFragment extends ListFragment {
	int mLastIndex;
	boolean mMultiPane;
	private DBHelper mDbManager;
	private MyDBHelper myDbManager;
	Cursor cursor = null;
	ArrayAdapter<ListnoCategoryDto> adapter;
	java.util.ArrayList<ListnoCategoryDto> arr_list;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDbManager = DBHelper.getInstance(getActivity());
		myDbManager = MyDBHelper.getInstance(getActivity());
		Log.d("print", "getInstance했음");
		arr_list = getFirstlist();

		adapter = new ArrayAdapter<ListnoCategoryDto>(getActivity(),
				android.R.layout.simple_list_item_activated_1, arr_list);
	}

	public ArrayList<ListnoCategoryDto> getFirstlist() {
		ArrayList<ListnoCategoryDto> fir_list = new java.util.ArrayList<ListnoCategoryDto>();
		cursor = mDbManager.selectCategory();
		if (cursor.moveToFirst()) {
			Log.d("print", "cursor했음");
			int n_list_no = cursor.getColumnIndex("list_no");
			int n_category = cursor.getColumnIndex("category");
			do {
				String list_no = cursor.getString(n_list_no);
				String category = cursor.getString(n_category);
				ListnoCategoryDto dto = new ListnoCategoryDto(list_no, category);
				fir_list.add(dto);
			} while (cursor.moveToNext());
			Log.d("print", fir_list.size() + "를 구할수 있네");
			cursor.close();
		}
		return fir_list;
	}

	public void setArr_list(java.util.ArrayList<ListnoCategoryDto> arr_list) {
		this.arr_list = arr_list;
		Log.d("print", this.arr_list.size() + "");
		adapter = new ArrayAdapter<ListnoCategoryDto>(getActivity(),
				android.R.layout.simple_list_item_activated_1, arr_list);
		setListAdapter(adapter);
		// adapter.notifyDataSetChanged();
		// adapter.notifyDataSetInvalidated();
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				final Intent modiintent = new Intent(getActivity(), QueryModiActivity.class);
				modiintent.putExtra("listno", arr_list.get(position).getList_no());
				modiintent.putExtra("category", arr_list.get(position).getCategory());
				new AlertDialog.Builder(getActivity()).setTitle("수정하기").setMessage("수정하러 갈까?").setPositiveButton("확인",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							if (whichButton == DialogInterface.BUTTON_POSITIVE) {
								startActivity(modiintent);
							}
						}
					}).setNegativeButton("취소", null).show();
				return true;
			}
		});
		if (savedInstanceState != null) {
			mLastIndex = savedInstanceState.getInt("lastindex");
		}
		View descPanel = getActivity().findViewById(
				R.id.ch15_multipanewidth_descpanel);
		if (descPanel != null && descPanel.getVisibility() == View.VISIBLE) {
			mMultiPane = true;
			onListItemClick(getListView(), null, mLastIndex, 0);
		}

	}

	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("lastindex", mLastIndex);
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mLastIndex = position;
		String listno = arr_list.get(position).getList_no();
		long a = myDbManager.insert(listno);
		Log.d("print", listno+"은 넣었을까?"+a);
		getListView().setItemChecked(position, true);
		if (mMultiPane) {
			FragmentManager fm = getFragmentManager();
			DescFragment df = (DescFragment) fm
					.findFragmentById(R.id.ch15_multipanewidth_descpanel);
			if (df == null || df.mNowIndex != position || df.list_no != listno) {
				df = DescFragment.newInstance(position, arr_list.get(position)
						.getList_no());

				fm.beginTransaction()
						.replace(R.id.ch15_multipanewidth_descpanel, df)
						.commit();
			}
		} else {
			Intent intent = new Intent(getActivity(), DescActivity.class);
			intent.putExtra("index", position);
			intent.putExtra("listno", arr_list.get(position).getList_no());
			startActivity(intent);
		}
	}

}